# -*- mode: snippet -*-
# name: swagger
# key: swagger
# --
-module(api_handler_$1).

-define(CALL_MODULE, mod_$1).
-include("api_helper/router.hrl").

trails() ->
    define_refs(),
    make_trails(routers()).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Routers
routers() ->
    [list_all()].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Types
define_refs() ->
    Props =
        #{
          <<"name">> =>
              #{type => <<"string">>,
                description => <<"Newspaper name">>},
          <<"description">> =>
              #{type => <<"string">>,
                description => <<"Newspaper description">>}
         },

    cowboy_swagger:add_definition(<<"some">>, Props).

%% APIs
list_all() ->
    QueryParams = [
                   #{name => <<"name">>,
                     in => query,
                     description => <<"description">>,
                     required => false,
                     default => <<"1">>,
                     type => string}
                  ],

    Metadata =
        #{post =>
              #{tags => ["$1"],
                description => <<"描述"/utf8>>,
                produces => ["application/json"],
                parameters => QueryParams,
                responses => #{
                               <<"200">> => #{
                                              description => <<"OK.">>,
                                              schema => #{
                                                          type => array,
                                                          items => cowboy_swagger:schema(<<"type">>)
                                                         }
                                             }
                              }
               }
         },

    {"$0path", Metadata}.
